﻿#include "frmlunarcalendarwidget.h"
#include "ui_frmlunarcalendarwidget.h"
#include <QDebug>
#include <QString>
#include "lunarLib/SolarUtil.h"
#include "lunarLib/LunarUtil.h"
#include <QKeyEvent>
#include "videoform.h"

frmLunarCalendarWidget::frmLunarCalendarWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmLunarCalendarWidget)
{
    ui->setupUi(this);
    //日历
    QDate date=ui->lunarCalendarWidget->getDate();
    solar=new Solar(date.year(),date.month(),date.day());
    this->initForm();
    ftimer.start(100);
    connect(&ftimer,SIGNAL(timeout()),this,SLOT(timer_timeout_slot()));
    updateLunar();
    //电源
    ui->battery->setValue(100);
}

frmLunarCalendarWidget::~frmLunarCalendarWidget()
{
    delete ui;
    delete solar;
}

void frmLunarCalendarWidget::initForm()
{
    ui->lunarCalendarWidget->setSelectType(LunarCalendarWidget::SelectType::SelectType_Image);
    //    ui->cboxWeekNameFormat->setCurrentIndex(2);
    updateTime();
}
void frmLunarCalendarWidget::updateTime()
{
    QDateTime curDateTime=QDateTime::currentDateTime();
    ui->timeLab->setText(curDateTime.toString("hh:mm:ss"));
    QDate date=ui->lunarCalendarWidget->getDate();
    if(date.day()!=solar->Day||date.month()!=solar->Month||date.year()!=solar->Year)
    {
        delete solar;
        solar=new Solar(date.year(),date.month(),date.day());
        updateLunar();
    }
}
void frmLunarCalendarWidget::timer_timeout_slot()
{
    updateTime();
}

void frmLunarCalendarWidget:: updateLunar()
{
    Lunar lunar=solar->lunar();
    std::vector<std::string>arr=  lunar.GetDayJi();

    //年月日星期
    std::string st=std::to_string(solar->Year)+"年"+std::to_string(solar->Month)+"月"+std::to_string(solar->Day)+"日星期"+SolarUtil::WEEK[solar->Week()];
    QString date=QString::fromStdString(st);
    ui->dateLab->setText(date);
    QString tmp=   QString::fromStdString(lunar.MonthInChinese()+"月");

    //农历
    ui->lunarMonth->setText(tmp);

    ui->lunarDay->setText(QString::fromStdString(lunar.DayInChinese()));


    // 输出日宜
    QStringList dayYi;
    int i=0;
    for(int i=0;i<lunar.GetDayYi().size();i++)
    {
        dayYi.append(QString::fromStdString(lunar.GetDayYi()[i]));
        if(i>=11)
            break;
    }
    //    for(const std::string& str : lunar.GetDayYi()) {
    //        dayYi.append(QString::fromStdString(str));
    //    }

    ui->yiArrLab->setText(dayYi.join(" "));


    // 输出日忌
    QStringList dayJi;
    for(const std::string& str : lunar.GetDayJi()) {
        dayJi.append(QString::fromStdString(str));
    }
    ui->jiArrLab->setText(dayJi.join(" "));

    //    // 输出干支信息
    //    qDebug() <<QString::fromStdString(lunar.YearInGanZhi()) << "年"
    //            << QString::fromStdString(lunar.MonthInGanZhi()) << "月"
    //            << QString::fromStdString(lunar.DayInGanZhi()) << "日" ;

    //吉神
    QStringList jishen ;
    for(const std::string& str : lunar.DayJiShen()) {
        jishen.append(QString::fromStdString(str));
    }
    ui->jishenArrLab->setText(jishen.join(" "));

    //凶神
    QStringList xiongsha ;
    for(const std::string& str : lunar.DayXiongSha()) {
        xiongsha.append(QString::fromStdString(str));
    }
    ui->xiongshenArrLab->setText(xiongsha.join(" "));

    //彭祖百忌：
    ui->peng1->setText(QString::fromStdString(lunar.PengZuGan()));
    ui->peng2->setText(QString::fromStdString(lunar.PengZuZhi()));

}

void frmLunarCalendarWidget::keyPressEvent(QKeyEvent *event)
{
    videoForm *videofm =new videoForm();
    videofm->show();

}



//void frmLunarCalendarWidget::on_cboxCalendarStyle_currentIndexChanged(int index)
//{
//    ui->lunarCalendarWidget->setCalendarStyle((LunarCalendarWidget::CalendarStyle)index);
//}

//void frmLunarCalendarWidget::on_cboxSelectType_currentIndexChanged(int index)
//{
//    ui->lunarCalendarWidget->setSelectType((LunarCalendarWidget::SelectType)index);
//}

//void frmLunarCalendarWidget::on_cboxWeekNameFormat_currentIndexChanged(int index)
//{
//    ui->lunarCalendarWidget->setWeekNameFormat((LunarCalendarWidget::WeekNameFormat)index);
//}

//void frmLunarCalendarWidget::on_ckShowLunar_stateChanged(int arg1)
//{
//    ui->lunarCalendarWidget->setShowLunar(arg1 != 0);
//}
